package com.ahp.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;



public class ConversionJavaToHtml {
	
	public static boolean conversionHtml(String urlStr,String htmlFileName){
		InputStream in;
	    URL url;
		try {
			//String urlString = "http://localhost:8080/Struts2Login/index.jsp";
			url = new URL(urlStr);
			java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
			connection = (java.net.HttpURLConnection) url.openConnection();
			//模拟成IE
			connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)");
			connection.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
			connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
			connection.setRequestProperty("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
			connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			connection.connect();
			in = connection.getInputStream();
			java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in,"utf-8"));
			StringBuffer content =new StringBuffer(); 
			String str=breader.readLine();
			while (str != null) {
				content.append(str);
				str = breader.readLine();
			}
			System.out.println(content.toString());
			toHtmlFile(content,htmlFileName);
			System.out.println("转换完成");
		} catch (Exception e) {
			System.out.println(e);
		}
		return true;
	}
	
	/**
	 * 生成html
	 * */
	private static boolean toHtmlFile(StringBuffer content,String htmlFileName){
		StringBuilder path=new StringBuilder();
		path.append("c:/html");//保存生成Html文件的目录
		/*
		String dateString =CharControl.dateToStringCustom(new Date(),"yyyyMMddHHmmss");
		path.append("/"+dateString.substring(0,4));
		path.append("/"+dateString.substring(4,6));
		path.append("/"+dateString.substring(6,8));
		*/
		File file=new File(path.toString());
		if(!file.exists()){//判断目录是否存在
			file.mkdirs();//建立
		}
		file = new File(path.append("/").append(htmlFileName).toString());
		try {
		file.createNewFile();//创建文件
		
		FileWriter out  = new FileWriter(file);
        //将输出文件流构造到缓存
        Writer bout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
        bout.write(content.toString());
        bout.close();
		
		/*OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
		System.out.println(content.toString());
		os.write(content.toString().getBytes());
		os.flush();
		os.close();*/
	   /* PrintStream printStream = new PrintStream(new FileOutputStream(file));
	    printStream.println(content.toString());//将字符串写入文件
	    printStream.close();*/
	    System.out.println(path.toString());
	   } catch (IOException e) {
	    
	    e.printStackTrace();
	   }
	   return true;
	}
	
	public static void main(String[] args) {
		ConversionJavaToHtml.conversionHtml("http://localhost:8080/ahp/user/listUser.action","index.html");

	}
}
